﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Patching
{
    public partial class PatchInvoiceDetailDescription : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnPatchSale_OnClick(object sender, EventArgs e)
        {
            InvoiceDetails ivc = new InvoiceDetails();
            ivc.Where.ItemType.Value = 2;
            ivc.Where.IMEI.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNotNull;
            ivc.Where.ProductID.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNotNull;
            ivc.Query.Load();

            Products p = new Products();
            p.LoadAll();
            
            for (int i = 0; i < ivc.RowCount; i++)
            {
                p.Filter = Products.ColumnNames.ID + "=" + ivc.s_ProductID;

                if (p.RowCount > 0)
                {
                    ivc.Description = string.Format("{0}<br/><span class='hintBold'>IMEI/Serial: {1}</span>", p.s_ProductName, ivc.s_IMEI);
                }

                ivc.MoveNext();
            }

            ivc.Save();
        }

        protected void btnPatch_OnClick(object sender, EventArgs e)
        {
            InvoiceDetails ivc = new InvoiceDetails();
            ivc.Where.ItemType.Value = 3;
            ivc.Where.IMEI.Operator = MyGeneration.dOOdads.WhereParameter.Operand.IsNotNull;
            ivc.Query.Load();

            View_RepairJobs job = new View_RepairJobs();
            job.LoadAll();

            RepairJobProblems probs = new RepairJobProblems();
            probs.LoadAll();

            for (int i = 0; i < ivc.RowCount; i++)
            {
                job.Filter = RepairJob.ColumnNames.InvoiceID + "=" + ivc.s_InvoiceID + " and " + RepairJob.ColumnNames.IMEI + "='" + ivc.s_IMEI + "'";
                if (job.RowCount > 0)
                {
                    string desc = "";
                    probs.Filter = RepairJobProblems.ColumnNames.RepairJobID + "=" + job.s_ID;
                    probs.Rewind();
                    for (int j = 0; j < probs.RowCount; j++)
                    {
                        desc += ", " + probs.s_Description;
                    }
                    ivc.Description = string.Format("{1}{2}<br/><span class='hintBold'>IMEI/Serial: {0}</span>", job.IMEI, job.s_BrandName + " " + job.s_ModelName, desc);

                }

                ivc.MoveNext();
            }

            ivc.Save();
        }
    }
}